//
//  JAllScreenCycleView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/31.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JAllScreenCycleView.h"
#import <UIImageView+YYWebImage.h>
#import "JHeadImgModel.h"

@implementation JAllScreenCycleView

- (instancetype)initWithFrame:(CGRect)frame imgBanners:(NSArray *)imgArr;
{
    if (self = [super initWithFrame:frame]) {
        if (!imgArr)
        {
            _imgDataArr = [NSMutableArray arrayWithArray:imgArr];
        }
        else
        {
            _imgDataArr = [NSMutableArray array];
        }
        _currentPage = 1;
        [self setUpView];
        [self setImgView];
    }
    return self;
}

- (void)setUpView {
    _cycleView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    _cycleView.delegate = self;
    _cycleView.pagingEnabled = YES;
    _cycleView.backgroundColor = [UIColor blackColor];
//    _cycleView.bounces = NO;
    [self addSubview:_cycleView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureMethod)];
    [_cycleView addGestureRecognizer:tap];
    
    _pageLabel = [YYLabel new];
    _pageLabel.textColor = [UIColor whiteColor];
    _pageLabel.top = self.height - 50;
    _pageLabel.centerX = self.centerX;
    _pageLabel.size = CGSizeMake(kWindowW, 30);
    _pageLabel.font = kFONT_TITLE(20);
    [self addSubview:_pageLabel];
    
}

- (void)setImgView {
    if (_imgDataArr.count == 0) {
        return;
    }
    
    if (_isClass) {
        for (int i = 0; i < _imgDataArr.count; i++) {
            JHeadImgModel *model = _imgDataArr[i];
            UIImageView *img = [[UIImageView alloc]init];
            img.centerY = self.centerY - 250/2.0;
            img.left = self.width * i;
            img.size = CGSizeMake(kWindowW, 250);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [img setImageWithURL:[NSURL URLWithString:model.ImgUrl] options:YYWebImageOptionUseNSURLCache];
                [_cycleView addSubview:img];
            });
            
        }
    }
    else
    {
        for (int i = 0; i < _imgDataArr.count; i++) {
            NSString *model = _imgDataArr[i];
            UIImageView *img = [[UIImageView alloc]init];
            img.top = (kWindowH - 470) / 2.0 - 20;
            img.left = self.width * i;
            img.size = CGSizeMake(kWindowW, 470);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [img setImageWithURL:[NSURL URLWithString:model] options:YYWebImageOptionUseNSURLCache];
                [_cycleView addSubview:img];
            });
            
        }
    }
    
    
    _cycleView.contentSize = CGSizeMake(self.width * _imgDataArr.count, self.height);
}

- (void)tapGestureMethod
{
    if (_cencleGesture) {
        _cencleGesture();
    }
}

- (void)setBennersArr:(NSArray *)bennersArr
{
    _bennersArr = bennersArr;
    _imgDataArr = [NSMutableArray arrayWithArray:_bennersArr];
    _pageLabel.text = [NSString stringWithFormat:@"1/%lu",(unsigned long)_bennersArr.count];
    [self setImgView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
    if (scrollView.contentOffset.x > (_currentPage - 1) * kWindowW) {
        if (_currentPage < _bennersArr.count) {
            _currentPage += 1;
        }
        
    }
    else
    {
        if (_currentPage > 1) {
            _currentPage -= 1;
        }
    }
    
    _pageLabel.text = [NSString stringWithFormat:@"%ld/%lu",(long)_currentPage,(unsigned long)_bennersArr.count];
}


@end
